package com.sk.cardeal.dialog;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.flipboard.bottomsheet.commons.BottomSheetFragment;
import com.sk.cardeal.R;
import com.sk.cardeal.adapter.BottomDataAdapter;
import com.sk.cardeal.callback.CountCallback;
import com.sk.cardeal.entity.NewPartsList;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * Created by sk on 2018/4/4.
 */

public class BottomDataFragment extends BottomSheetFragment implements BaseQuickAdapter.OnItemChildClickListener {

    @BindView(R.id.data_ryc)
    RecyclerView dataRyc;
    Unbinder unbinder;
    ArrayList<NewPartsList.ResultListBean.PartsQualityListBean> dataList = new ArrayList<>();
    private CountCallback callback;

    public CountCallback getCallback() {
        return callback;
    }

    public void setCallback(CountCallback callback) {
        this.callback = callback;
    }

    public static BottomDataFragment newInstance(ArrayList<NewPartsList.ResultListBean.PartsQualityListBean> dataList) {
        Bundle args = new Bundle();
        args.putParcelableArrayList("data", dataList);
        BottomDataFragment fragment = new BottomDataFragment();
        fragment.setArguments(args);
        return fragment;
    }


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_data_bottom, container, false);
        unbinder = ButterKnife.bind(this, view);
        return view;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        List<NewPartsList.ResultListBean.PartsQualityListBean> data = getArguments().getParcelableArrayList("data");
        dataList.addAll(data);
        dataRyc.setLayoutManager(new LinearLayoutManager(getActivity()));
        BottomDataAdapter adapter = new BottomDataAdapter(R.layout.item_bottom_data, dataList);
        dataRyc.setAdapter(adapter);
        adapter.setOnItemChildClickListener(this);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    @Override
    public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
        switch (view.getId()) {
            case R.id.add:
                if(callback!=null){
                    callback.count(1,dataList.get(position).getGroupPos(),dataList.get(position).getChildPos(),dataList.get(position));
                }
                adapter.notifyItemChanged(position);
                break;
            case R.id.reduce:
                int count2 = dataList.get(position).getWantCount();
                if (count2 < 1)
                    return;

                if(callback!=null){
                    callback.count(0,dataList.get(position).getGroupPos(),dataList.get(position).getChildPos(),dataList.get(position));
                }
                dataList.get(position).setWantCount(count2 - 1);
                if (count2 == 1) {
                    dataList.remove(position);
                    adapter.notifyItemRemoved(position);
                } else {
                    adapter.notifyItemChanged(position);
                }

                break;
        }
    }
}
